﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation.Collections;
using Xiaowei.Diagnostics;
using Xiaowei.Players.XiaoweiPlayerLib;

namespace Xiaowei.Players
{
    class XiaoweiPlayer : IPlayer
    {
        private Queue<KeyValuePair<PlayerCommandType, object>> commandList = new Queue<KeyValuePair<PlayerCommandType, object>>();
        private static Lazy<XiaoweiPlayer> lazyInnerPlayer = new Lazy<XiaoweiPlayer>(new XiaoweiPlayer());

        public event EventHandler<PlayStateCombine> PlayerStateChanged;

        public static XiaoweiPlayer Instance => lazyInnerPlayer.Value;

        private XiaoweiPlayer()
        {
            PlayStateCombine = new PlayStateCombine()
            {
                Title = "",
                ID = "",
                PlayerState = PlayerState.Stop
            };

            XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.PlayerStateChanged += XiaoweiPlayerAudio_PlayerStateChanged;
        }

        private void XiaoweiPlayerAudio_PlayerStateChanged(object sender, EventArgs e)
        {
            PlayStateCombine.PlayerState = XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.PlayerState;
            PlayStateCombine.Title = "";
            PlayStateCombine.ID = "";
            if (XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.XiaoweiPlayerList!=null&& XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.XiaoweiPlayerList?.CurrentItem!=null)
            {
                PlayStateCombine.Title = XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.XiaoweiPlayerList.CurrentItem.Title;
                PlayStateCombine.ID = XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.XiaoweiPlayerList.CurrentItem.PlayId;
            }
            PlayerStateChanged?.Invoke(this,PlayStateCombine);
        }

        public bool IsInstalled { get;} = true;
        public bool IsLaunched
        {
            get
            {
                if (!XiaoweiPlayerManager.Instance.IsClosed)
                {
                    return true;
                }
                return false;
            }
        }

        public PlayStateCombine PlayStateCombine { get; private set; }

        /// <summary>
        /// 保留用于添加播放器最大化功能
        /// </summary>
        public void Launch()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Launch, null));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.Launch");
            if (!IsLaunched)
            {
                XiaoweiPlayerManager.Instance.Launch();
            }
        }

        public void Play(object item)
        {
            Debug.WriteLine($"XiaoweiPlayer.Play");
            if (isSuspend)
            {
                commandList.Clear();
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Play, item));
                return;
            }
            if (item is IXiaoweiPlayerList list)
            {
                XiaoweiPlayerManager.Instance.OperateList(list, ListOperation.Replace);
            }
            Launch();
            Debug.WriteLine("Play(object item) end");
        }

        public void Play()
        {
            if(isSuspend)
            {
                resumePauseState = false;
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.Play");
            XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.Play();
        }
        public void Pause()
        {
            if (isSuspend)
            {
                resumePauseState = true;
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.Pause");
            XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.Pause();
        }
        public void Stop()
        {
            if (isSuspend)
            {
                resumePauseState = true;
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.Stop");
            XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.Stop();
        }
        public void FastForward(int seconds)
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>( PlayerCommandType.FastForward,seconds));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.FastForward");
            XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.FastForward(seconds);
        }

        public void FastRewind(int seconds)
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.FastRewind, seconds));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.FastRewind");
            XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.FastRewind(seconds);
        }
        public void Next()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Next, null));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.Next");
            XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.Next();
        }
        public void PlayByListLoop()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayByListLoop, null));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.PlayByListLoop");
            XiaoweiPlayerManager.Instance.SetPlayMode(PlayMode.LoopPlay);
        }

        public void PlayByOrder()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayByOrder, null));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.PlayByOrder");
            XiaoweiPlayerManager.Instance.SetPlayMode(PlayMode.OrderPlay);
        }

        public void PlayByRandom()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayByRandom, null));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.PlayByRandom");
            XiaoweiPlayerManager.Instance.SetPlayMode(PlayMode.RandomPlay);
        }

        public void PlayBySingleLoop()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.PlayBySingleLoop, null));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.PlayBySingleLoop");
            XiaoweiPlayerManager.Instance.SetPlayMode(PlayMode.SinglePlay);
        }

        public void Prev()
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.Prev, null));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.Prev");
            XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.Prev();
        }

        public void SeekTo(int seconds)
        {
            if (isSuspend)
            {
                commandList.Enqueue(new KeyValuePair<PlayerCommandType, object>(PlayerCommandType.SeekTo, seconds));
                return;
            }
            Debug.WriteLine("XiaoweiPlayer.SeekTo");
            XiaoweiPlayerManager.Instance.XiaoweiPlayerAudio.SeekTo(seconds);
        }
        bool resumePauseState = false;
        bool isSuspend = true;
        public void Suspend()
        {
            if (!isSuspend)
            {
                Debug.WriteLine("XiaoweiPlayer.Suspend");
                commandList.Clear();
                resumePauseState = PlayStateCombine.PlayerState != PlayerState.Play;
                Pause();
                isSuspend = true;
                XiaoweiPlayerManager.Instance.Suspend();
            }
        }
        public void Resumed()
        {
            if (!isSuspend) return;
            isSuspend = false;
            Debug.WriteLine("XiaoweiPlayer.Resumed");
            while (commandList.Count > 0)
            {
                var command = commandList.Dequeue();
                Debug.WriteLine($"XiaoweiPlayer.Resumed, key:{command.Key}");
                switch (command.Key)
                {
                    case PlayerCommandType.Launch:
                        Launch();
                        break;
                    case PlayerCommandType.Play:
                        Play((IXiaoweiPlayerList)command.Value);
                        break;
                    case PlayerCommandType.Prev:
                        Prev();
                        break;
                    case PlayerCommandType.Next:
                        Next();
                        break;
                    case PlayerCommandType.FastForward:
                        FastForward((int)command.Value);
                        break;
                    case PlayerCommandType.FastRewind:
                        FastRewind((int)command.Value);
                        break;
                    case PlayerCommandType.SeekTo:
                        SeekTo((int)command.Value);
                        break;
                    case PlayerCommandType.PlayByOrder:
                        PlayByOrder();
                        break;
                    case PlayerCommandType.PlayByRandom:
                        PlayByRandom();
                        break;
                    case PlayerCommandType.PlayByListLoop:
                        PlayByListLoop();
                        break;
                    case PlayerCommandType.PlayBySingleLoop:
                        PlayBySingleLoop();
                        break;
                    default:
                        break;
                }
            }
            if (!resumePauseState)
            {
                Play();
            }
            XiaoweiPlayerManager.Instance.Resume();
            Debug.WriteLine("XiaoweiPlayer.Resumed, End");
        }
        public void Resumed2()
        {
            if (!isSuspend) return;
            isSuspend = false;
            commandList.Clear();
            Debug.WriteLine("XiaoweiPlayer.Resumed2");
            if (!resumePauseState)
            {
                Play();
            }
            XiaoweiPlayerManager.Instance.Resume();
            Debug.WriteLine("XiaoweiPlayer.Resumed2 End");
        }
    }
}
